
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;


public class PantallaBaixaAparell {
	//private Model model;
	private Aparell aparell;	
	private JLabel etiqueta;
	private Gestor gestor;
	private JComboBox autors;
	private JButton btnSortir;
	private JButton btnBorrar;
	private PantallaMenu pantallaMenu;
	private JInternalFrame consultaInterna;
	private JDesktopPane desktoppane;	
	
	
	public PantallaBaixaAparell(PantallaMenu unaPantallaMenu){
		pantallaMenu= unaPantallaMenu;
		inicialitza();
		activarMenus(false);
		crearComponents();
		crearGestorsDisseny();
	}
	
	private void inicialitza(){
		gestor = new Gestor(this);
	}
	
	private void crearComponents(){
		
		//etiqueta publicacions
		etiqueta= new JLabel("Tria Aparell:");
		
		//combo autors
		autors= new JComboBox(getAutors());
		autors.addActionListener(gestor);
		autors.setActionCommand("triAutor");
		//boto acceptar
		btnSortir= new JButton("Sortir");
		btnSortir.addActionListener(gestor);
		btnSortir.setActionCommand("Sortir");
		
		//boto Borrar
		btnBorrar= new JButton("Eliminar");
		btnBorrar.addActionListener(gestor);
		btnBorrar.setActionCommand("Eliminar");
	}
	
	private void crearGestorsDisseny(){
		//crear desktopPane
		desktoppane= new JDesktopPane();
		//crear frameIntern
		consultaInterna= new JInternalFrame("Consulta de salas");
		
		//afegir frameInern al desktoppane
		desktoppane.add(consultaInterna);
		
		//crear gestor de posicionament del frameIntern
		GridBagLayout gbl= new GridBagLayout();
		GridBagConstraints gbc= new GridBagConstraints();
		consultaInterna.setLayout(gbl);
		
		//posicionament dels elements al frameIntern
		//TAULA AUTOR
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.gridwidth=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor= GridBagConstraints.WEST;
		gbc.weightx=0;
		gbc.weighty=0;
		gbl.setConstraints(etiqueta, gbc);
		consultaInterna.add(etiqueta);
		
		gbc.gridx=2;
		gbc.gridy=2;
		gbc.gridwidth=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor= GridBagConstraints.WEST;
		gbc.weightx=0;
		gbc.weighty=0;
		gbl.setConstraints(autors, gbc);
		consultaInterna.add(autors);
		
		
		//boto Sortir
		gbc.gridx=3;
		gbc.gridy=4;
		gbc.gridwidth=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.EAST;
		gbc.weightx=0;
		gbc.weighty=0;
		gbl.setConstraints(btnSortir, gbc);
		consultaInterna.add(btnSortir);
		//boto Borrar
		gbc.gridx=4;
		gbc.gridy=4;
		gbc.gridwidth=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.EAST;
		gbc.weightx=0;
		gbc.weighty=0;
		gbl.setConstraints(btnBorrar, gbc);
		consultaInterna.add(btnBorrar);
		
		//titol del frame INTERN
		TitledBorder tb=BorderFactory.createTitledBorder("Eliminar salas");
		tb.setTitleColor(Color.BLUE);
		consultaInterna.setBorder(tb);
	
		//Afegir frameIntern al JFrame
		pantallaMenu.getContentPane().add(consultaInterna,BorderLayout.NORTH);
		//visualitzar frame intern
		consultaInterna.setVisible(true);
	}
	
	
	private Object[] getAutors(){
		int cont=0;
		Collection<Aparell> aparells= new ArrayList<Aparell>();
	
			Manager man = new Manager();

			aparells=man.obtenirAparells();
			return aparells.toArray();
	}
	
	private void activarMenus(Boolean activar){
		pantallaMenu.getMenuConsultes().setEnabled(activar);
		pantallaMenu.getMenuGestio().setEnabled(activar);
	}
	
	public class Gestor implements ActionListener{
		private PantallaBaixaAparell pba;
		public Gestor(PantallaBaixaAparell pantallaBaixaSala){
			pba= pantallaBaixaSala;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Object o= arg0.getSource();
			if(o instanceof JButton){
				JButton boto= (JButton) o;
				if(boto.getActionCommand().equals("Sortir")){
					activarMenus(true);
					consultaInterna.dispose();
				}
				if(boto.getActionCommand().equals("Eliminar")){
					Manager gce = new Manager();
					aparell= (Aparell) autors.getSelectedItem();
					if(aparell!=null){
						aparell.eliminarBD();
					}
					
					activarMenus(false);					
				}
			}
			if(o instanceof JComboBox){
				
				
			}
		}

		public void netejaPantalla(){			
		}
		
	}


}
